function addRootTask(task_details_json) {
	var task_details = JSON.parse(task_details_json);
	var p_id = task_details['project_id'];
	var t_id = task_details['task_id'];
	var title = task_details['title'];
	var description = task_details['description'];
	if (getActiveProjectID() != p_id) {
		// don't add task in the wrong project
		return false;
	}

	var task_template = template_RootTaskItem(t_id, title, description);
	var tasks_list = "#RootTasksList";

	$(tasks_list).prepend(task_template);
	$(tasks_list + " li:first-child .RootTaskTitle").hover(function() {
				$(this).find(".ToolButton").show();
			},
			function() {
				$(this).find(".ToolButton").hide();
			});
	$("#" + t_id + " .RootTaskDetails .RootTaskDescription").hover(function() {
				$(this).find(".ToolButtonStickToRightTopCorner").show();
			},
			function() {
				$(this).find(".ToolButtonStickToRightTopCorner").hide();
			})
	$("#" + t_id + " .RootTaskTitle .RootTaskTitleText").editable(function(value, settings) {
		//TODO: insert submit code here
		return value.trim();
	}, {
		width: "none",
		data: function(value, settings) {
			return value.trim();
		},
		onblur: "cancel",
		event: "EnableTaskTitleEditing"
	});
}

function addSubTask(task_details_json) {
	var task_details = JSON.parse(task_details_json);
	var p_id = task_details['project_id'];
	var t_id = task_details['task_id'];
	var parent_id = task_details['task_parent'];
	var title = task_details['title'];
	var description = task_details['description'];

	if (getActiveProjectID() != p_id) {
		// don't add task in the wrong project
		return false;
	}

	var parent_task = $("#" + parent_id).find(".RootTaskContent");
	if (parent_task.length == 0)
		var content = ".TaskContent";
	else
		var content = ".RootTaskContent";

	var task_template = template_SubTaskItem(t_id, title, description);

//	var content = ".RootTaskContent";
//	if (parent_id != null)
//		content = ".TaskContent";

//	if ($("#" + parent_id + " > " + content).height() == 0)
//		return false;

	var tasks_list = "#" + parent_id + " > " + content + "> .TasksContainer > .TasksList";

	$(tasks_list).prepend(task_template);
	$(tasks_list + " li:first-child .TaskTitle").hover(function() {
				$(this).find(".ToolButton").show();
			},
			function() {
				$(this).find(".ToolButton").hide();
			});
	$("#" + t_id + " .TaskDetails .TaskDescription").hover(function() {
				$(this).find(".ToolButtonStickToRightTopCorner").show();
			},
			function() {
				$(this).find(".ToolButtonStickToRightTopCorner").hide();
			})
	$("#" + t_id + " .TaskTitle .TaskTitleText").editable(function(value, settings) {
		//TODO: insert submit code here
		return value.trim();
	}, {
		width: "none",
		data: function(value, settings) {
			return value.trim();
		},
		onblur: "cancel",
		event: "EnableTaskTitleEditing"
	});
}

function createTask(p_id) {
//	var p_id = getActiveProjectID();
	var task_details = {
		"project_id": p_id,
		"task_parent": null,
		"title": $("#NewTaskFormWrapper form input[name=title]").val(),
		"description": $("#NewTaskFormWrapper form .CreationFormEditableDiv").html()
	};
	pyForm.create_task(JSON.stringify(task_details));
	clearFormElements("#NewTaskForm");
	removeNewTaskForm();
}

function createSubTask(t_li) {
	var p_id = getActiveProjectID();
	var cur_datetime = (new Date()).toGMTString().replace(new RegExp(" |,|:", 'g'), "_");
	var task_details = {
		"project_id": p_id,
		"task_parent": t_li.attr("id"),
		"title": $("#NewTaskFormWrapper form input[name=title]").val(),
		"description": $("#NewTaskFormWrapper form .CreationFormEditableDiv").html()
	};
	pyForm.create_task(JSON.stringify(task_details));

	clearFormElements("#NewTaskForm");
	removeNewTaskForm();
}

function deleteTask(li) {
	/*When "delete task" button pressed get id of this milestone and make
	 * a request to delete this task. When confirmation received,
	 * remove task function will be called.
	 * */
	var t_id = li.attr("id");
	//TODO: ask python do delete t ask
	removeTask(t_id);
}

function removeTask(p_id) {
	/*Remove task from the list by given ID.*/
	var activeProjectID = getActiveProjectID();
	var taskToBeRemoved = $("#" + p_id);
	taskToBeRemoved.fadeOut(200, function() {
		$(this).remove();
	});
}

function enableTaskTitleEditing(task) {
	task.find(".TaskTitle .TaskTitleText").trigger("EnableTaskTitleEditing");
}

function enableTaskDescriptionEditing(task_desc) {
	task_desc.attr("contentEditable", "true");
}

function onKeyPressedInTaskDescriptionField(e, t) {
	var key = e.keyCode || e.which;
	if(e.ctrlKey && (key == 10)) //TODO: why 10??
	{
		//TODO: submit data here
		$(t).attr("contentEditable", "false");
	}
}
